<?php
namespace Module\Controller;

use Common\Controller\AdminController;
use Common\Model\KeywordModel;

class KeywordController extends AdminController {

    protected $module;

    public function __init() {
        $class        = 'Addons\\' . MODULE . '\Keyword';
        $this->module = new $class;
    }

    // 关键词列表
    public function lists() {
        $keywordModel = new KeywordModel();
        // 根据模块取关键词
        $list = $keywordModel->where(['module' => MODULE])->select();

        $this->list = $list;
        $this->display();
    }

    // 关键词回复
    public function set() {
        // 获取模块实例
        $mo  = I('get.mo');
        $rid = I('get.rid', 0, 'intval');
        if (IS_POST) {

            $data           = I('post.');
            $data['module'] = $mo;

            // 大于0表示修改
            if ($rid > 0) {
                $data['rid'] = $rid;
            }

            $this->store(new KeywordModel(), $data, function ($ret) use ($data, $rid) {
                // 关键词表的主键
                $rs['rid']     = $rid > 0 ? $rid : $ret['data'];
                $rs['content'] = $data['content'];

                $this->module->submit($rs);
            });

            exit;
        }

        if ($rid > 0) {
            $info = (new KeywordModel())->find($rid);
            $this->info = $info;
        }

        // 回复内容
        $this->content = $this->module->form($rid);
        $this->display();
    }
}